using NSEMSolver
using Documenter
using DocumenterTools: Themes
using Literate

# Set up the documentation build
DocMeta.setdocmeta!(NSEMSolver, :DocTestSetup, :(using NSEMSolver); recursive=true)

# Generate examples using Literate.jl
examples_dir = joinpath(@__DIR__, "..", "examples")
output_dir = joinpath(@__DIR__, "src", "examples")
mkpath(output_dir)

# Convert Julia example files to markdown
for file in readdir(examples_dir)
    if endswith(file, ".jl") && !startswith(file, ".")
        input_file = joinpath(examples_dir, file)
        if isfile(input_file)
            try
                Literate.markdown(input_file, output_dir; 
                    documenter=true, 
                    credit=false,
                    execute=false)  # Set to true if you want to execute examples
                println("Generated documentation for $file")
            catch e
                @warn "Could not process $file: $e"
            end
        end
    end
end

# Define the documentation structure
makedocs(;
    modules=[NSEMSolver],
    authors="NSEMSolver.jl Contributors",
    repo="https://github.com/username/NSEMSolver.jl/blob/{commit}{path}#{line}",
    sitename="NSEMSolver.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://username.github.io/NSEMSolver.jl",
        edit_link="main",
        assets=String[],
        sidebar_sitename=true,
        collapselevel=1,
    ),
    pages=[
        "Home" => "index.md",
        "Installation" => "installation.md",
        "Quick Start" => "quickstart.md",
        "User Guide" => [
            "Basic Usage" => "guide/basic_usage.md",
            "Mesh Generation" => "guide/mesh_generation.md",
            "Solver Configuration" => "guide/solver_config.md",
            "Boundary Conditions" => "guide/boundary_conditions.md",
            "Post-processing" => "guide/postprocessing.md",
        ],
        "Examples" => [
            "Lid-Driven Cavity" => "examples/lid_driven_cavity.md",
            "Channel Flow" => "examples/channel_flow.md",
            "Cylinder Flow" => "examples/cylinder_flow.md",
        ],
        "Theory" => [
            "Mathematical Background" => "theory/mathematical_background.md",
            "Spectral Element Method" => "theory/spectral_element_method.md",
            "Navier-Stokes Equations" => "theory/navier_stokes.md",
            "Numerical Methods" => "theory/numerical_methods.md",
        ],
        "API Reference" => [
            "Core Types" => "api/core.md",
            "Mesh Generation" => "api/geometry.md",
            "Linear Algebra" => "api/linear_algebra.md",
            "Solvers" => "api/solvers.md",
            "Spectral Methods" => "api/spectral.md",
            "Analysis Tools" => "api/analysis.md",
            "Extensions" => "api/extensions.md",
        ],
        "Development" => [
            "Contributing" => "development/contributing.md",
            "Developer Setup" => "development/setup.md",
            "Code Style" => "development/style.md",
            "Testing" => "development/testing.md",
            "Benchmarking" => "development/benchmarking.md",
            "Release Process" => "development/releases.md",
        ],
        "Performance" => "performance.md",
        "Changelog" => "changelog.md",
    ],
    checkdocs=:exports,
    linkcheck=true,
    strict=true,
)

deploydocs(;
    repo="github.com/username/NSEMSolver.jl",
    devbranch="main",
    push_preview=true,
)